var CommonJs = (function () {
    function commonJs() {
        this.version = 1.0
        this.usedFor = "common js write here"
    }

    /**
     * 分页处理，包括跳转，搜索跳转
     * total-总页数 cur-当前页 tips-提示信息 hrefn-搜索之后的分页函数
     */
    commonJs.prototype.page = function (total, cur, tips, hrefn, justPage) {
        var _this = this;
        total = parseInt(total);

        if (total == 0 || isNaN(total)) {
            total = 1;
            cur = 1;
        }

        $("#page").bootstrapPaginator({
            currentPage: cur,
            totalPages: total,
            bootstrapMajorVersion: 3,
            size: "small",
            shouldShowPage: function (type, page, current) {
                if (justPage && ["first", "prev", "next", "last"].indexOf(type) >= 0) {
                    return false
                } else {
                    return true
                }
            },
            // itemContainerClass: function (type, page, current) {
            //     if(type == "page"){
            //
            //     }
            // },
            onPageClicked: function (e, originalEvent, type, page) {
                if (page != cur) {
                    hrefn(page);
                }
            }
        });

        //跳转
        $('#page_sBtn').click(function () {
            var num = parseInt($('#page_skip').val());
            var total = $('#page_num').text();

            if (isNaN(num) || num == '') {
                // _this.tiperr(tips[0]);
                alert(tips[0]);
                $('#page_skip').val('');
                return;
            } else if (num > total || num <= 0) {
                // _this.tiperr(tips[1]);
                alert(tips[1]);
                $('#page_skip').val('');
                return;
            } else if (num == cur) {
                // _this.tiperr(tips[2]);\
                alert(tips[2]);
                $('#page_skip').val('');
                return;
            }

            $("#page").bootstrapPaginator("show", num);

            hrefn(num);
        });

        //支持回车
        $('#page_skip').keydown(function (event) {
            if (event.keyCode == 13) {
                $('#page_sBtn').trigger('click');
            }
        });

        //如果只有一页就隐藏分页
        if (total == 1) {
            $('.tab_footer').css("visibility", "hidden");
        } else {
            $('.tab_footer').css("visibility", "visible");
        }
    }

    /**
     * 分页处理，包括跳转，搜索跳转----适用于get请求
     * total-总页数 cur-当前页 tips-提示信息 hrefn-搜索之后的分页函数
     */
    commonJs.prototype.getpage = function (total, cur, tips) {

        var _this = this;
        total = parseInt(total);

        if (total == 0 || isNaN(total)) {
            total = 1;
            cur = 1;
        }

        /*使用url.min.js.插件,只需要切换page即可*/
        function pages(page) {
            var url = URI(document.location.href).removeQuery("p").addQuery("p", page);
            window.location.href = url.toString();
        }

        $("#page").bootstrapPaginator({
            currentPage: cur,
            totalPages: total,
            bootstrapMajorVersion: 3,
            size: "small",
            onPageClicked: function (e, originalEvent, type, page) {
                if (page != cur) {
                    pages(page);
                }
            }
        });

        //跳转
        $('#page_sBtn').click(function () {
            var num = parseInt($('#page_skip').val());
            var total = $('#page_num').text();

            if (isNaN(num) || num == '') {
                alert(tips[0]);
                $('#page_skip').val('');
                return;
            } else if (num > total || num <= 0) {
                alert(tips[1]);
                $('#page_skip').val('');
                return;
            } else if (num == cur) {
                alert(tips[2]);
                $('#page_skip').val('');
                return;
            }

            $("#page").bootstrapPaginator("show", num);

            pages(num);
        });

        //支持回车
        $('#page_skip').keydown(function (event) {
            if (event.keyCode == 13) {
                $('#page_sBtn').trigger('click');
            }
        });

        //如果只有一页就隐藏分页
        if (total == 1) {
            $('.tab_footer').css("visibility", "hidden");
        } else {
            $('.tab_footer').css("visibility", "visible");
        }
    }

    //js format
    String.prototype.format= function(){
        var args = Array.prototype.slice.call(arguments);
        var count=0;
        return this.replace(/%s/g,function(s,i){
            return args[count++];
        });
    }


    commonJs.prototype.add_tr=function(obj) {
        var tr = $(obj).parent().parent();
        tr.after(tr.clone());
    }

    commonJs.prototype.up_tr=function(obj) {
        var rowName =$(obj).parent().parent().attr("name")
        var prevName=$(obj).parent().parent().prev().attr("name");
        if(rowName== prevName){
            let tr = $(obj).parent().parent();
            let prev = tr.prev();
            tr.insertBefore(prev);
        }
    }

    commonJs.prototype.del_tr=function(obj) {
        if (confirm("确定要删除吗？")) {
            var rowName =$(obj).parent().parent().attr("name");
            let len=  $("div[name='"+rowName+"']").length;
            if (len<= 1) {
                alert("最少保留一行数据")
                return
            }
            let tr = $(obj).parent().parent();
            tr.remove()
            return true
        } else {
            return false
        }

    }

    return new commonJs()
})()


